using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNAssist;

namespace XNAssistTest
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class XNAssistTest : Game
    {
        GraphicsDeviceManager _graphics;
        SpriteBatch _spriteBatch;

        private Texture2D _nineTileTexture;

        private SpriteFont _font;

        private Texture2D _cursorTexture;

        private NineTile _nineTile;

        private TextWrappedLabel _label;

        private Cursor _cursor;

        private const int TextBoxHeight = 192;

        private int _letters;

        private const string OriginalText =
            "You don't know me. Hopefully you never will. I have been hiding in the shadows all of my life, watching. Waiting. Finally, my time has come. Never before have I had a chance to do what I am about to do: I hope that it is memorable.";

        private const float TimeBetweenLetters = 0.05f, FastTimeBetweenLetters = 0.005f;

        private Timer _letterTimer;

        public XNAssistTest()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            _graphics.PreferredBackBufferWidth = 1366;
            _graphics.PreferredBackBufferHeight = 768;
            _graphics.ApplyChanges();

            _nineTileTexture = Content.Load<Texture2D>("FuturisticNineTile");

            _font = Content.Load<SpriteFont>("Trebuchet");

            _cursorTexture = Content.Load<Texture2D>("Cursor");

            //_nineTile = new NineTile(_nineTileTexture, 64, 64, 0.5f, 0.5f);
            _nineTile = new NineTile(_nineTileTexture, 256, 256, 0.125f);

            _label = new TextWrappedLabel(_font, _graphics.PreferredBackBufferWidth - 32, OriginalText);

            _letterTimer = new Timer(TimeBetweenLetters, true);

            _cursor = new Cursor(_cursorTexture, new Vector2(0.05f), Vector2.Zero, true);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {

        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                Exit();

            _letterTimer.Update(gameTime);

            _letterTimer.TriggerTime = Keyboard.GetState().IsKeyDown(Keys.Enter)
                ? FastTimeBetweenLetters
                : TimeBetweenLetters;

            if (_letterTimer.TimerFinished)
            {
                _letterTimer.Reset();
                _letters++;
            }

            if (_letters >= OriginalText.Length)
                _letters = OriginalText.Length;

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            _spriteBatch.Begin();

            var textBoxUpperLeft = new Vector2(0, _graphics.PreferredBackBufferHeight - TextBoxHeight);

            _nineTile.Draw(_spriteBatch, textBoxUpperLeft, _graphics.PreferredBackBufferWidth, TextBoxHeight, Color.White);

            const int offset = 32;

            _label.TextWidth = _graphics.PreferredBackBufferWidth - (offset * 2);
            _label.Text = OriginalText.Substring(0, _letters);
            _label.Draw(_spriteBatch, textBoxUpperLeft + new Vector2(offset), Color.White);

            _cursor.Draw(_spriteBatch, Color.Aquamarine);

            _spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
